<template>
  <div class='container'>
    <el-dialog
  :title="pageTitle"
  :visible="dialogShow"
  @close="close"
  width="20%"
  >
 <el-form :model="form" ref="form" label-width="70px" :rules="rules">
  <el-form-item label="学科名称"    >
    <el-input v-model.trim="form.subjectName" autocomplete="off"></el-input>
  </el-form-item>
</el-form>
<el-switch
  style="display: block"
  v-model="form.isFrontDisplay"
  active-color="#13ce66"
  inactive-color="#ff4949"
  inactive-text="是否显示">
</el-switch>
  <span slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="submit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import { add, update, detail } from '@/api/hmmm/subjects.js'
export default {
  name: 'SubjectAdd',
  props: ['dialogShow', 'pageTitle'],
  data () {
    return {
      form: {
        subjectName: '',
        isFrontDisplay: false,
        id: null
      },
      rules: {
        subject: [{ required: true, message: '学科名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async hanldeEditForm (id) {
      this.form.id = id
      const { data } = await detail(this.form)
      this.form.subjectName = data.subjectName
      this.form.isFrontDisplay = data.isFrontDisplay === 1
    },
    close () {
      this.$refs.form.resetFields()
      this.form.subjectName = ''
      this.$emit('update:dialogShow', false)
    },
    async submit () {
      try {
        await this.$refs.form.validate()
        this.form.isFrontDisplay = this.form.isFrontDisplay ? 1 : 0

        if (this.form.id) {
          await update(this.form)
          this.$message.success('修改成功')
        } else {
          await add(this.form)
          this.$message.success('添加成功')
        }
        this.close()
        this.$parent.initData()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.dialog-footer{
  margin-left:135px ;
}
</style>
